package com.story.api.moduler.song.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.story.api.common.persistence.dao.UserChannelMapper;
import com.story.api.common.persistence.dao.VedioCategoryMapper;
import com.story.api.common.persistence.model.UserChannel;
import com.story.api.common.persistence.model.VedioDetail;
import com.story.api.config.properties.UpyunProperties;
import com.story.api.moduler.song.dao.ChannelItemDao;
import com.story.api.moduler.song.service.ChannelService;
import com.story.api.moduler.song.transfer.vo.MediaResourceVo;
import com.story.api.moduler.upyun.service.UpyunService;
import com.story.core.util.UriUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangbo
 * @date 2019-08-08 14:00
 */
@Service
public class ChannelServiceImpl implements ChannelService {

    private static final Long EXPIRE_TIME = 36000L;

    @Autowired
    private UserChannelMapper userChannelMapper;

    @Autowired
    private ChannelItemDao channelItemDao;

    @Autowired
    private UpyunProperties upyunProperties;

    @Autowired
    private UpyunService upyunService;

    @Autowired
    private VedioCategoryMapper vedioCategoryMapper;

    /**
     * 绑定用户与频道的关系
     * @param userId
     * @param channelIds
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bindUserAndChannel(Integer userId, List<Integer> channelIds) {
        for (Integer id : channelIds) {
            UserChannel userChannel = new UserChannel();
            userChannel.setUserId(userId);
            userChannel.setChannelId(id);
            UserChannel existUserChannel = userChannelMapper.selectOne(userChannel);
            if(existUserChannel != null) {
                continue;
            }
            userChannelMapper.insert(userChannel);
        }
    }

    /**
     * 解绑关系
     * @param userId
     * @param channelIds
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void unbindUserAndChannel(Integer userId, List<Integer> channelIds) {
        for (Integer id : channelIds) {
            userChannelMapper.delete(new EntityWrapper<UserChannel>().eq("user_id",userId).eq("channel_id",id));
        }
    }

    /**
     * 频道下的媒体资源列表
     * @param channelId
     * @return
     */
    @Override
    public List<MediaResourceVo> channelResources(Integer channelId) {
        List<VedioDetail> vedioDetailList = channelItemDao.channelRadioList(channelId);
        List<MediaResourceVo> mediaResourceVoList = new ArrayList<>();
        for(VedioDetail vedioDetail : vedioDetailList) {
            MediaResourceVo mediaResourceVo = new MediaResourceVo();
            BeanUtils.copyProperties(vedioDetail,mediaResourceVo);
            mediaResourceVo.setPicPath(addTokenUrl(vedioDetail.getLogoPath()));
            mediaResourceVo.setPlayPath(upyunProperties.getDomain() + vedioDetail.getFilePath());
            mediaResourceVo.setPlayCount(50000+mediaResourceVo.getPlayCount());
            mediaResourceVo.setProductName(vedioCategoryMapper.selectById(vedioDetail.getCateId()).getName());
            mediaResourceVoList.add(mediaResourceVo);
        }
        return mediaResourceVoList;
    }

    /**
     * 对url添加token,防止盗链
     * @param url
     * @return
     */
    private String addTokenUrl(String url) {
        return upyunProperties.getDomain() + url + "?_upt=" + upyunService.getTokenSign( UriUtil.getUri(url),EXPIRE_TIME);
    }
}
